/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.util;

import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

/**
 * The Class ZipUtil.
 *
 * @author Xi Wei
 */
public class ZipUtil {

	/**
	 * 执行压缩操作.
	 *
	 * @param srcPathName            需要被压缩的文件/文件夹
	 * @param destFile the dest file
	 * @return the string
	 */
	public static String zipFile(String srcPathName, String destFile) {

		File zipFile = new File(destFile);
		zipFile.delete();
		File srcdir = new File(srcPathName);
		if (!srcdir.exists()) {
			throw new RuntimeException(srcPathName + "不存在！");
		}

		Project prj = new Project();
		Zip zip = new Zip();
		zip.setProject(prj);
		zip.setDestFile(zipFile);
		FileSet fileSet = new FileSet();
		fileSet.setProject(prj);
		fileSet.setDir(srcdir);
		// fileSet.setIncludes("**/*.java"); //包括哪些文件或文件夹
		// eg:zip.setIncludes("*.java");
		// fileSet.setExcludes(...); //排除哪些文件或文件夹
		zip.addFileset(fileSet);
		zip.execute();
		return zipFile.getName();
	}

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {

		ZipUtil.zipFile("/opt/test", "/opt/test/test.zip");
	}
}
